import { Global, Module } from '@nestjs/common';// 导入Redis服务
import { ConfigModule, ConfigService } from '@nestjs/config';
import { RedisModule as NestRedisModule, RedisModuleOptions } from '@nestjs-modules/ioredis';
import redisConfig from '../config/redis.config';
import { RedisService } from './redis.service';
// 将当前模块标记为全局模块，使其在整个应用中可用
@Global()
@Module({
  imports: [
    ConfigModule.forFeature(redisConfig),
    NestRedisModule.forRootAsync({
      imports: [ConfigModule], // 导入配置模块
      inject: [ConfigService], // 注入配置服务
      useFactory: (configService: ConfigService): RedisModuleOptions => {
        // 从配置服务中获取redis配置
        const { host, port, password, db, prefix, ttl, retryStrategy, reconnectOnError } =
          configService.get('redis');
        return {
          type: 'single', // 使用单实例模式
          options: {
            host,
            port,
            password,
            db,
            keyPrefix: prefix,
            retryStrategy,
            reconnectOnError,
          },
        };
      },
    }),
  ],
  providers: [RedisService],
  exports: [RedisService],
})
export class RedisModule { } 